package ru.lenwp.model;

import ru.lenwp.operation.admin.AdminOperationService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Lenar on 22.06.2014.
 */
public class Client extends User{
    private ArrayList<Account> accountList = new ArrayList<Account>();

    public ArrayList<Account> getAccountList() {
        return accountList;
    }

    public void setAccountList(ArrayList<Account> accountList) {
        this.accountList = accountList;
    }

    public void addAccaunt(Account acc){
        if (acc == null) return;
        acc.setClient(this);
        accountList.add(acc);
    }

    public static User createClient(){
        User client = new Client();
        Scanner sc = new Scanner(System.in);
        System.out.println("Введите логин ");
        client.setLogin(sc.nextLine());
        System.out.println("Введите пароль ");
        client.setPassword(sc.nextLine());
        System.out.println("Введите Имя ");
        client.setFirstName(sc.nextLine());
        System.out.println("Введите Фамилию ");
        client.setLastName(sc.nextLine());
        System.out.println("Введите Отчество ");
        client.setSurName(sc.nextLine());
        boolean isCorrect = false;
        while(!isCorrect) {
            try {
                isCorrect = true;
                System.out.println("Введите дату рождения ");
                client.setDate(new SimpleDateFormat("dd-MM-yyyy").parse(sc.nextLine()));
            } catch (ParseException e) {
                System.out.println("Не верно введена дата ");
                isCorrect = false;
            }
        }
        System.out.println("Введите email ");
        //Pattern pattern = Pattern.compile("\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w(2,4)+)");
        String str = sc.nextLine();
        //Matcher matcher = pattern.matcher(str);
        //boolean ft = matcher.matches();
        //if(ft) {
            client.setEmail(str);
        //}
        return client;
    }

    public static Client createUserByName(String login, String firstName, String lastName,
                                          String surName, Date date, String email){
        Client res = new Client();
        res.setLogin(login);
        res.setFirstName(firstName);
        res.setLastName(lastName);
        res.setSurName(surName);
        res.setDate((java.sql.Date) date);
        res.setEmail(email);
        Account acc = new Account();
        acc.setClient(res);
        res.accountList.add(acc);
        return res;
    }
}
